
###############################################
########## JEPS OPTIMAL PERSUASION ############
###############################################

## Optimal Persuasion under Confirmation Bias: Theory and Evidence from a Registered Report
## Journal of Experimental Political Science
## Author: Love Christensen
## website: lovechristensen.com
## Contact: love.christensen@gmail.com

################################################
############ REPLICATION MATERIAL ##############
################################################

################ FILE 2: TABLES ################

## It is important to run 1_experiment_prep_and_analysis.R before this file, since that file contains all the models which are called in the table scripts.

## The table script takes an input of models (model.list), variables (var.list) and their corresponding names (name.list) and outputs a table to be imported directly in the .tex file.
## To increase customization, every table preamble is written manually in the file.
## I comment the code of the first table in some detail. The remaining tables are constructed in exactly the same way.
## All tables are saved in "../output/tables" folder


## TABLE ORDER:

# 1. Table 2: "Effect of Prediction Distance on Updating and Uncertainty"
# 2. Appendix Table 1: "Heterogeneity: Copartisan Sender"
# 3. Appendix Table 2: "Heterogeneity: Prior Certainty"
# 4. Appendix Table 3: "Dropping Potential Floor Observations"
# 5. Appendix Table 4: "Controlling for Prior Beliefs"

####################################################
##################### TABLE 2 ######################
####################################################

## "Effect of Prediction Distance on Updating and Uncertainty"

## Define the ordered vector of model names that are included in the table
model.list <- c(
  "m1_u",
  "m2_u",
  "m3_u",
  "m6_u",
  "m7_u",
  "m8_u",
  "m1_c",
  "m2_c",
  "m4_c",
  "m5_c"
)

## Provide the ordered vector of variables from the above models that should be printed in the table
## Variables not included in the vector are not printed in the table
var.list <- c("I(preddistvalue)",
              "I(preddistvalue^2)",
              "I(preddistvalue^3)",
              "preddistvalue == 0TRUE")

## Provide a vector of names for these variables
## These must be in the same order as for the var.list vector
name.list <- c("Distance", 
               "Distance$^2$",
               "Distance$^3$",
               "Prior = Prediction (Dummy)")


## Combine the two vectors into a data frame
combined.list <- data.frame(var.list,
                            name.list)

## define the output name of the table
tab.name <- "tab_updating_cred"

## define the path to the output folder
tab.path <- paste('../output/tables/', tab.name, '.tex', sep = "")

## create the file
## all information from the table head, table body and table foot are now appended to this file
cat("", file = tab.path)

## define table header
cat( "\\begin{table}[H]
     \\caption{Effect of Prediction Distance on Updating and Uncertainty}
     \\label{tab:results}  
     \\begin{center}
     \\begin{adjustbox}{max width=1\\textwidth}
     \\begin{tabular}{l ccccccc cccc}
     \\toprule %%%%%%%%%%%%%%%%%%%%%%%%%
     &  \\multicolumn{6}{c}{Updating} & \\multicolumn{4}{c}{Credibility} \\\\     \\cmidrule(lr){2-7} \\cmidrule(lr){8-11}
     &  \\multicolumn{3}{c}{Pre-Registered} & \\multicolumn{3}{c}{Post Hoc} & \\multicolumn{2}{c}{Pre-Registered} & \\multicolumn{2}{c}{Post Hoc} \\\\
     \\cmidrule(lr){2-4} \\cmidrule(lr){5-7} \\cmidrule(lr){8-9} \\cmidrule(lr){10-11}
     & Linear & Quadratic & Cubic & Linear & Quadratic & Cubic & Linear & Quadratic & Linear & Quadratic \\\\
     \\midrule %%%%%%%%%%%%%%%%%%%%%%%%% \n",
     append = F, file = tab.path)

#################################################################
#################################################################


## The following is a loop which - for each variable and model - 
## 1. checks the p-values of the estimates and assigns the significance symbols corresponding to the p-value
## significance stars
## 2. Outputs the variable name, the variable coefficient and the standard error for each variable X model combination in the above vectors
for(i in 1:length(name.list)){
  
  sig.list <- vector(length = length(model.list))
  sig.list.symbol <- vector(length = length(model.list))
  
  ## significance symbols
  for(j in 1:length(model.list)){
    sig.list[j] <- eval(parse(text = paste(paste(model.list[j], "$p.value['", var.list[i], "']", sep = ""))))
    #sig.list.symbol[j] <- ifelse(sig.list[j] <= .1, "$ \\dagger $", "") # 10%
    sig.list.symbol[j] <- ifelse(sig.list[j] <= .05, "$^{*}$", "") # 5%
    sig.list.symbol[j] <- ifelse(sig.list[j] <= .01, "$^{**}$", sig.list.symbol[j]) # 1%
    sig.list.symbol[j] <- ifelse(sig.list[j] <= .001, "$^{***}$", sig.list.symbol[j]) # 0.1%
    sig.list.symbol[j] <- ifelse(is.na(sig.list[j]) == TRUE, "", sig.list.symbol[j]) # if NA
  }
  #################################################################
  #################################################################

  #################################################################
  #################################################################
  ## variable name
  cat(paste(combined.list[var.list == var.list[i], "name.list"], sep = ""),
      append = TRUE, file = tab.path)
  #################################################################
  #################################################################
  
  #################################################################
  #################################################################
  
  ## COEFFICIENTS
  cat(
    for(m in 1:length(model.list)){
      cat(" & ", 
          sprintf(
            ifelse(is.na(eval(parse(text = paste(model.list[m], "$coefficients['", var.list[i], "']", sep = "")))) == TRUE,
                   "%s",
                   "%.2f"), 
            ifelse(is.na(eval(parse(text = paste(model.list[m], "$coefficients['", var.list[i], "']", sep = "")))) == TRUE,
                   " ",
                   eval(parse(text = paste(model.list[m], "$coefficients['", var.list[i], "']", sep = "")))
            )
          ),
          eval(parse(text = paste("sig.list.symbol[", m, "]", sep = ""))),
          sep = "", append = TRUE, file = tab.path)
    }
    , " \\\\ \n ", 
    append = TRUE, file = tab.path)
  #################################################################
  #################################################################
  
  #################################################################
  #################################################################
  ## STANDARD ERRORS
  cat(
    for(m in 1:length(model.list)){
      cat(" & ",
          ifelse(is.na(eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = "")))) == TRUE,
                 "",
                 "("),
          sprintf(
            ifelse(is.na(eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = "")))) == TRUE,
                   "%s",
                   "%.2f"), 
            ifelse(is.na(eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = "")))) == TRUE,
                   " ",
                   eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = ""))))
          ),
          ifelse(is.na(eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = "")))) == TRUE,
                 "",
                 ")"), 
          sep = "", append = TRUE, file = tab.path
      )
    }
    , " \\\\ \n \\addlinespace[0.2cm] ",
    append = TRUE, file = tab.path
  )
  
}
#################################################################
#################################################################


#################################################################
#################################################################

## This writes the AIC scores corresponding to the models
cat("\\midrule AIC", append = TRUE, file = tab.path)
for(m in 1:length(model.list)){
  cat(" & ", 
      sprintf("%.0f", eval(parse(text = paste(model.list[m], "_aic", sep = "")))),
      sep = "", append = TRUE, file = tab.path)
}
cat(" \\\\ \n", sep = "", append = TRUE, file = tab.path)
#################################################################
#################################################################

#################################################################
#################################################################

## This writes the number of observations corresponding to the models

cat("Observations", append = TRUE, file = tab.path)
for(m in 1:length(model.list)){
  cat(" & ", 
      sprintf("%.0f", eval(parse(text = paste(model.list[m], "$N", sep = "")))),
      sep = "", append = TRUE, file = tab.path)
}
cat(" \\\\ \n", sep = "", append = TRUE, file = tab.path)

#################################################################
#################################################################

## Finally, we declare the table footer
cat("\\bottomrule %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \\end{tabular}
    \\end{adjustbox}
    \\caption*{\\footnotesize{\\textbf{Note:} OLS estimates with robust standard errors within parentheses. Prediction distance ranges from 0 to 3, in increments of 0.1 corresponding to a loss of 100,000 jobs. Updating is the difference between the posterior and prior belief expressed in millions of jobs. Credibility ranges from 0 to 10 and higher values mean more credible predictions.  \\\\\\hspace{\\textwidth}
    $^* p <0.05, ^{**} p<0.01, ^{***} p<0.001$}}
    \\end{center}
    \\end{table}", 
    append = TRUE, file= tab.path)

####################################################
################# APPENDIX TABLE 1 #################
####################################################

## "Heterogeneity: Copartisan Sender"

## variable labels for table (in order)
name.list <- c("Distance", 
               "Distance$^2$",
               "Distance$^3$",
               "Copartisan $\\times$ Distance",
               "Copartisan $\\times$ Distance$^2$",
               "Copartisan $\\times$ Distance$^3$",
               "Copartisan",
               "Prior = Prediction (Dummy)")


## variable names in regressions (same order)
var.list <- c("I(preddistvalue)",
              "I(preddistvalue^2)",
              "I(preddistvalue^3)",
              "I(preddistvalue):copartisan == 1TRUE",
              "copartisan == 1TRUE:I(preddistvalue^2)",
              "copartisan == 1TRUE:I(preddistvalue^3)",
              "copartisan == 1TRUE",
              "preddistvalue == 0TRUE")


combined.list <- data.frame(var.list,
                            name.list)

## models to include
model.list <- c(
  "m2_pu",
  "m3_pu",
  "m4_pu",
  "m2_pc",
  "m3_pc"
)

tab.name <- "tab_updating_cred_copartisan"
tab.path <- paste('../output/tables/', tab.name, '.tex', sep = "")

cat("", file = tab.path)
cat( "\\begin{table}[H]
     \\caption{Heterogeneity: Copartisan Sender}
     \\label{tab:copartisan}  
     \\begin{center}
     \\begin{adjustbox}{max width=1\\textwidth}
     \\begin{tabular}{l ccccc}
     \\toprule %%%%%%%%%%%%%%%%%%%%%%%%%
     &  \\multicolumn{3}{c}{Updating} & \\multicolumn{2}{c}{Credibility} \\\\
     \\midrule %%%%%%%%%%%%%%%%%%%%%%%%% \n",
     append = F, file = tab.path)

#################################################################
#################################################################
## significance stars

for(i in 1:length(name.list)){
  sig.list <- vector(length = length(model.list))
  sig.list.symbol <- vector(length = length(model.list))
  for(j in 1:length(model.list)){
    sig.list[j] <- eval(parse(text = paste(paste(model.list[j], "$p.value['", var.list[i], "']", sep = ""))))
    #sig.list.symbol[j] <- ifelse(sig.list[j] <= .1, "$ \\dagger $", "") # 10%
    sig.list.symbol[j] <- ifelse(sig.list[j] <= .05, "$^{*}$", "") # 5%
    sig.list.symbol[j] <- ifelse(sig.list[j] <= .01, "$^{**}$", sig.list.symbol[j]) # 1%
    sig.list.symbol[j] <- ifelse(sig.list[j] <= .001, "$^{***}$", sig.list.symbol[j]) # 0.1%
    sig.list.symbol[j] <- ifelse(is.na(sig.list[j]) == TRUE, "", sig.list.symbol[j]) # if NA
  }
  #################################################################
  #################################################################
  
  
  
  
  #################################################################
  #################################################################
  ## variable name
  cat(paste(combined.list[var.list == var.list[i], "name.list"], sep = ""),
      append = TRUE, file = tab.path)
  #################################################################
  #################################################################
  
  #################################################################
  #################################################################
  ## COEFFICIENTS
  cat(
    for(m in 1:length(model.list)){
      cat(" & ", 
          sprintf(
            ifelse(is.na(eval(parse(text = paste(model.list[m], "$coefficients['", var.list[i], "']", sep = "")))) == TRUE,
                   "%s",
                   "%.2f"), 
            ifelse(is.na(eval(parse(text = paste(model.list[m], "$coefficients['", var.list[i], "']", sep = "")))) == TRUE,
                   " ",
                   eval(parse(text = paste(model.list[m], "$coefficients['", var.list[i], "']", sep = "")))
            )
          ),
          eval(parse(text = paste("sig.list.symbol[", m, "]", sep = ""))),
          sep = "", append = TRUE, file = tab.path)
    }
    , " \\\\ \n ", 
    append = TRUE, file = tab.path)
  #################################################################
  #################################################################
  
  #################################################################
  #################################################################
  ## STANDARD ERRORS
  cat(
    for(m in 1:length(model.list)){
      cat(" & ",
          ifelse(is.na(eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = "")))) == TRUE,
                 "",
                 "("),
          sprintf(
            ifelse(is.na(eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = "")))) == TRUE,
                   "%s",
                   "%.2f"), 
            ifelse(is.na(eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = "")))) == TRUE,
                   " ",
                   eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = ""))))
          ),
          ifelse(is.na(eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = "")))) == TRUE,
                 "",
                 ")"), 
          sep = "", append = TRUE, file = tab.path
      )
    }
    , " \\\\ \n \\addlinespace[0.2cm] ",
    append = TRUE, file = tab.path
  )
  
}
#################################################################
#################################################################




#################################################################
#################################################################
## AIC SCORES
cat("\\midrule AIC", append = TRUE, file = tab.path)
for(m in 1:length(model.list)){
  cat(" & ", 
      sprintf("%.0f", eval(parse(text = paste(model.list[m], "_aic", sep = "")))),
      sep = "", append = TRUE, file = tab.path)
}
cat(" \\\\ \n", sep = "", append = TRUE, file = tab.path)
#################################################################
#################################################################

#################################################################
#################################################################
## OBSERVATIONS
cat("Observations", append = TRUE, file = tab.path)
for(m in 1:length(model.list)){
  cat(" & ", 
      sprintf("%.0f", eval(parse(text = paste(model.list[m], "$N", sep = "")))),
      sep = "", append = TRUE, file = tab.path)
}
cat(" \\\\ \n", sep = "", append = TRUE, file = tab.path)
#################################################################
#################################################################

cat("\\bottomrule %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \\end{tabular}
    \\end{adjustbox}
    \\caption*{\\footnotesize{\\textbf{Note:} OLS estimates with robust standard errors within parentheses. Higher values for updating implies manufacturing job increases and higher values for credibility means more credible predictions.  \\\\\\hspace{\\textwidth}
    $^* p <0.05, ^{**} p<0.01, ^{***} p<0.001$}}
    \\end{center}
    \\end{table}", 
    append = TRUE, file= tab.path)

####################################################
################# APPENDIX TABLE 2 #################
####################################################

## "Heterogeneity: Prior Certainty"

## variable labels for table (in order)
name.list <- c("Distance", 
               "Distance$^2$",
               "Distance$^3$",
               "Certain $\\times$ Distance",
               "Certain $\\times$ Distance$^2$",
               "Certain $\\times$ Distance$^3$",
               "Certain",
               "Prior = Prediction (Dummy)")


## variable names in regressions (same order)
var.list <- c("I(preddistvalue)",
              "I(preddistvalue^2)",
              "I(preddistvalue^3)",
              "I(preddistvalue):certain == 1TRUE",
              "certain == 1TRUE:I(preddistvalue^2)",
              "certain == 1TRUE:I(preddistvalue^3)",
              "certain == 1TRUE",
              "preddistvalue == 0TRUE")


combined.list <- data.frame(var.list,
                            name.list)

## models to include
model.list <- c(
  "m2_cu",
  "m3_cu",
  "m4_cu",
  "m2_cc",
  "m3_cc"
)

tab.name <- "tab_updating_cred_certain"
tab.path <- paste('../output/tables/', tab.name, '.tex', sep = "")

cat("", file = tab.path)
cat( "\\begin{table}[H]
     \\caption{Heterogeneity: Prior Certainty}
     \\label{tab:certain}  
     \\begin{center}
     \\begin{adjustbox}{max width=1\\textwidth}
     \\begin{tabular}{l ccccc}
     \\toprule %%%%%%%%%%%%%%%%%%%%%%%%%
     &  \\multicolumn{3}{c}{Updating} & \\multicolumn{2}{c}{Credibility} \\\\
     \\midrule %%%%%%%%%%%%%%%%%%%%%%%%% \n",
     append = F, file = tab.path)

#################################################################
#################################################################
## significance stars

for(i in 1:length(name.list)){
  sig.list <- vector(length = length(model.list))
  sig.list.symbol <- vector(length = length(model.list))
  for(j in 1:length(model.list)){
    sig.list[j] <- eval(parse(text = paste(paste(model.list[j], "$p.value['", var.list[i], "']", sep = ""))))
    #sig.list.symbol[j] <- ifelse(sig.list[j] <= .1, "$ \\dagger $", "") # 10%
    sig.list.symbol[j] <- ifelse(sig.list[j] <= .05, "$^{*}$", "") # 5%
    sig.list.symbol[j] <- ifelse(sig.list[j] <= .01, "$^{**}$", sig.list.symbol[j]) # 1%
    sig.list.symbol[j] <- ifelse(sig.list[j] <= .001, "$^{***}$", sig.list.symbol[j]) # 0.1%
    sig.list.symbol[j] <- ifelse(is.na(sig.list[j]) == TRUE, "", sig.list.symbol[j]) # if NA
  }
  #################################################################
  #################################################################
  
  
  
  
  #################################################################
  #################################################################
  ## variable name
  cat(paste(combined.list[var.list == var.list[i], "name.list"], sep = ""),
      append = TRUE, file = tab.path)
  #################################################################
  #################################################################
  
  #################################################################
  #################################################################
  ## COEFFICIENTS
  cat(
    for(m in 1:length(model.list)){
      cat(" & ", 
          sprintf(
            ifelse(is.na(eval(parse(text = paste(model.list[m], "$coefficients['", var.list[i], "']", sep = "")))) == TRUE,
                   "%s",
                   "%.2f"), 
            ifelse(is.na(eval(parse(text = paste(model.list[m], "$coefficients['", var.list[i], "']", sep = "")))) == TRUE,
                   " ",
                   eval(parse(text = paste(model.list[m], "$coefficients['", var.list[i], "']", sep = "")))
            )
          ),
          eval(parse(text = paste("sig.list.symbol[", m, "]", sep = ""))),
          sep = "", append = TRUE, file = tab.path)
    }
    , " \\\\ \n ", 
    append = TRUE, file = tab.path)
  #################################################################
  #################################################################
  
  #################################################################
  #################################################################
  ## STANDARD ERRORS
  cat(
    for(m in 1:length(model.list)){
      cat(" & ",
          ifelse(is.na(eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = "")))) == TRUE,
                 "",
                 "("),
          sprintf(
            ifelse(is.na(eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = "")))) == TRUE,
                   "%s",
                   "%.2f"), 
            ifelse(is.na(eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = "")))) == TRUE,
                   " ",
                   eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = ""))))
          ),
          ifelse(is.na(eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = "")))) == TRUE,
                 "",
                 ")"), 
          sep = "", append = TRUE, file = tab.path
      )
    }
    , " \\\\ \n \\addlinespace[0.2cm] ",
    append = TRUE, file = tab.path
  )
  
}
#################################################################
#################################################################




#################################################################
#################################################################
## AIC SCORES
cat("\\midrule AIC", append = TRUE, file = tab.path)
for(m in 1:length(model.list)){
  cat(" & ", 
      sprintf("%.0f", eval(parse(text = paste(model.list[m], "_aic", sep = "")))),
      sep = "", append = TRUE, file = tab.path)
}
cat(" \\\\ \n", sep = "", append = TRUE, file = tab.path)
#################################################################
#################################################################

#################################################################
#################################################################
## OBSERVATIONS
cat("Observations", append = TRUE, file = tab.path)
for(m in 1:length(model.list)){
  cat(" & ", 
      sprintf("%.0f", eval(parse(text = paste(model.list[m], "$N", sep = "")))),
      sep = "", append = TRUE, file = tab.path)
}
cat(" \\\\ \n", sep = "", append = TRUE, file = tab.path)
#################################################################
#################################################################

cat("\\bottomrule %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \\end{tabular}
    \\end{adjustbox}
    \\caption*{\\footnotesize{\\textbf{Note:} OLS estimates with robust standard errors within parentheses. Higher values for updating implies manufacturing job increases and higher values for credibility means more credible predictions.  \\\\\\hspace{\\textwidth}
    $^* p <0.05, ^{**} p<0.01, ^{***} p<0.001$}}
    \\end{center}
    \\end{table}", 
    append = TRUE, file= tab.path)


####################################################
############## APPENDIX TABLE 3 ####################
####################################################

## "Dropping Potential Floor Observations"

## variable labels for table (in order)
name.list <- c("Distance", 
               "Distance$^2$",
               "Distance$^3$",
               "Prior = Prediction (Dummy)")


## variable names in regressions (same order)
var.list <- c("I(preddistvalue)",
              "I(preddistvalue^2)",
              "I(preddistvalue^3)",
              "preddistvalue == 0TRUE")


combined.list <- data.frame(var.list,
                            name.list)

## models to include
model.list <- c(
  "m1_u_rob",
  "m2_u_rob",
  "m3_u_rob",
  "m6_u_rob",
  "m7_u_rob",
  "m8_u_rob",
  "m1_c_rob",
  "m2_c_rob",
  "m4_c_rob",
  "m5_c_rob"
)

tab.name <- "tab_updating_cred_floor"
tab.path <- paste('../output/tables/', tab.name, '.tex', sep = "")

cat("", file = tab.path)
cat( "\\begin{table}[H]
     \\caption{Dropping Potential Floor Observations}
     \\label{tab:results_floor}  
     \\begin{center}
     \\begin{adjustbox}{max width=1\\textwidth}
     \\begin{tabular}{l ccccccc cccc}
     \\toprule %%%%%%%%%%%%%%%%%%%%%%%%%
     &  \\multicolumn{6}{c}{Updating} & \\multicolumn{4}{c}{Credibility} \\\\     \\cmidrule(lr){2-7} \\cmidrule(lr){8-11}
     &  \\multicolumn{3}{c}{Pre-Registered} & \\multicolumn{3}{c}{Post Hoc} & \\multicolumn{2}{c}{Pre-Registered} & \\multicolumn{2}{c}{Post Hoc} \\\\
     \\midrule %%%%%%%%%%%%%%%%%%%%%%%%% \n",
     append = F, file = tab.path)

#################################################################
#################################################################
## significance stars

for(i in 1:length(name.list)){
  sig.list <- vector(length = length(model.list))
  sig.list.symbol <- vector(length = length(model.list))
  for(j in 1:length(model.list)){
    sig.list[j] <- eval(parse(text = paste(paste(model.list[j], "$p.value['", var.list[i], "']", sep = ""))))
    #sig.list.symbol[j] <- ifelse(sig.list[j] <= .1, "$ \\dagger $", "") # 10%
    sig.list.symbol[j] <- ifelse(sig.list[j] <= .05, "$^{*}$", "") # 5%
    sig.list.symbol[j] <- ifelse(sig.list[j] <= .01, "$^{**}$", sig.list.symbol[j]) # 1%
    sig.list.symbol[j] <- ifelse(sig.list[j] <= .001, "$^{***}$", sig.list.symbol[j]) # 0.1%
    sig.list.symbol[j] <- ifelse(is.na(sig.list[j]) == TRUE, "", sig.list.symbol[j]) # if NA
  }
  #################################################################
  #################################################################
  
  
  
  
  #################################################################
  #################################################################
  ## variable name
  cat(paste(combined.list[var.list == var.list[i], "name.list"], sep = ""),
      append = TRUE, file = tab.path)
  #################################################################
  #################################################################
  
  #################################################################
  #################################################################
  ## COEFFICIENTS
  cat(
    for(m in 1:length(model.list)){
      cat(" & ", 
          sprintf(
            ifelse(is.na(eval(parse(text = paste(model.list[m], "$coefficients['", var.list[i], "']", sep = "")))) == TRUE,
                   "%s",
                   "%.2f"), 
            ifelse(is.na(eval(parse(text = paste(model.list[m], "$coefficients['", var.list[i], "']", sep = "")))) == TRUE,
                   " ",
                   eval(parse(text = paste(model.list[m], "$coefficients['", var.list[i], "']", sep = "")))
            )
          ),
          eval(parse(text = paste("sig.list.symbol[", m, "]", sep = ""))),
          sep = "", append = TRUE, file = tab.path)
    }
    , " \\\\ \n ", 
    append = TRUE, file = tab.path)
  #################################################################
  #################################################################
  
  #################################################################
  #################################################################
  ## STANDARD ERRORS
  cat(
    for(m in 1:length(model.list)){
      cat(" & ",
          ifelse(is.na(eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = "")))) == TRUE,
                 "",
                 "("),
          sprintf(
            ifelse(is.na(eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = "")))) == TRUE,
                   "%s",
                   "%.2f"), 
            ifelse(is.na(eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = "")))) == TRUE,
                   " ",
                   eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = ""))))
          ),
          ifelse(is.na(eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = "")))) == TRUE,
                 "",
                 ")"), 
          sep = "", append = TRUE, file = tab.path
      )
    }
    , " \\\\ \n \\addlinespace[0.2cm] ",
    append = TRUE, file = tab.path
  )
  
}
#################################################################
#################################################################




#################################################################
#################################################################
## AIC SCORES
cat("\\midrule AIC", append = TRUE, file = tab.path)
for(m in 1:length(model.list)){
  cat(" & ", 
      sprintf("%.0f", eval(parse(text = paste(model.list[m], "_aic", sep = "")))),
      sep = "", append = TRUE, file = tab.path)
}
cat(" \\\\ \n", sep = "", append = TRUE, file = tab.path)
#################################################################
#################################################################

#################################################################
#################################################################
## OBSERVATIONS
cat("Observations", append = TRUE, file = tab.path)
for(m in 1:length(model.list)){
  cat(" & ", 
      sprintf("%.0f", eval(parse(text = paste(model.list[m], "$N", sep = "")))),
      sep = "", append = TRUE, file = tab.path)
}
cat(" \\\\ \n", sep = "", append = TRUE, file = tab.path)
#################################################################
#################################################################

cat("\\bottomrule %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \\end{tabular}
    \\end{adjustbox}
    \\caption*{\\footnotesize{\\textbf{Note:} OLS estimates with robust standard errors within parentheses. Higher values for updating implies manufacturing job increases and higher values for credibility means more credible predictions.  \\\\\\hspace{\\textwidth}
    $^* p <0.05, ^{**} p<0.01, ^{***} p<0.001$}}
    \\end{center}
    \\end{table}", 
    append = TRUE, file= tab.path)




####################################################
################# APPENDIX TABLE 4 #################
####################################################

## "Controlling for Prior Beliefs"

## variable labels for table (in order)
name.list <- c("Distance", 
               "Distance$^2$",
               "Distance$^3$",
               "Prior = Prediction (Dummy)")


## variable names in regressions (same order)
var.list <- c("I(preddistvalue)",
              "I(preddistvalue^2)",
              "I(preddistvalue^3)",
              "preddistvalue == 0TRUE")


combined.list <- data.frame(var.list,
                            name.list)

## models to include
model.list <- c(
  "m1_u_prior",
  "m2_u_prior",
  "m3_u_prior",
  "m6_u_prior",
  "m7_u_prior",
  "m8_u_prior",
  "m1_c_prior",
  "m2_c_prior",
  "m4_c_prior",
  "m5_c_prior"
)

tab.name <- "tab_updating_prior"
tab.path <- paste('../output/tables/', tab.name, '.tex', sep = "")

cat("", file = tab.path)
cat( "\\begin{table}[H]
     \\caption{Controlling for Prior Beliefs}
     \\label{tab:results_prior}  
     \\begin{center}
     \\begin{adjustbox}{max width=1\\textwidth}
     \\begin{tabular}{l ccccccc cccc}
     \\toprule %%%%%%%%%%%%%%%%%%%%%%%%%
     &  \\multicolumn{6}{c}{Updating} & \\multicolumn{4}{c}{Credibility} \\\\     \\cmidrule(lr){2-7} \\cmidrule(lr){8-11}
     &  \\multicolumn{3}{c}{Pre-Registered} & \\multicolumn{3}{c}{Post Hoc} & \\multicolumn{2}{c}{Pre-Registered} & \\multicolumn{2}{c}{Post Hoc} \\\\
     \\midrule %%%%%%%%%%%%%%%%%%%%%%%%% \n",
     append = F, file = tab.path)

#################################################################
#################################################################
## significance stars

for(i in 1:length(name.list)){
  sig.list <- vector(length = length(model.list))
  sig.list.symbol <- vector(length = length(model.list))
  for(j in 1:length(model.list)){
    sig.list[j] <- eval(parse(text = paste(paste(model.list[j], "$p.value['", var.list[i], "']", sep = ""))))
    #sig.list.symbol[j] <- ifelse(sig.list[j] <= .1, "$ \\dagger $", "") # 10%
    sig.list.symbol[j] <- ifelse(sig.list[j] <= .05, "$^{*}$", "") # 5%
    sig.list.symbol[j] <- ifelse(sig.list[j] <= .01, "$^{**}$", sig.list.symbol[j]) # 1%
    sig.list.symbol[j] <- ifelse(sig.list[j] <= .001, "$^{***}$", sig.list.symbol[j]) # 0.1%
    sig.list.symbol[j] <- ifelse(is.na(sig.list[j]) == TRUE, "", sig.list.symbol[j]) # if NA
  }
  #################################################################
  #################################################################
  
  
  
  
  #################################################################
  #################################################################
  ## variable name
  cat(paste(combined.list[var.list == var.list[i], "name.list"], sep = ""),
      append = TRUE, file = tab.path)
  #################################################################
  #################################################################
  
  #################################################################
  #################################################################
  ## COEFFICIENTS
  cat(
    for(m in 1:length(model.list)){
      cat(" & ", 
          sprintf(
            ifelse(is.na(eval(parse(text = paste(model.list[m], "$coefficients['", var.list[i], "']", sep = "")))) == TRUE,
                   "%s",
                   "%.2f"), 
            ifelse(is.na(eval(parse(text = paste(model.list[m], "$coefficients['", var.list[i], "']", sep = "")))) == TRUE,
                   " ",
                   eval(parse(text = paste(model.list[m], "$coefficients['", var.list[i], "']", sep = "")))
            )
          ),
          eval(parse(text = paste("sig.list.symbol[", m, "]", sep = ""))),
          sep = "", append = TRUE, file = tab.path)
    }
    , " \\\\ \n ", 
    append = TRUE, file = tab.path)
  #################################################################
  #################################################################
  
  #################################################################
  #################################################################
  ## STANDARD ERRORS
  cat(
    for(m in 1:length(model.list)){
      cat(" & ",
          ifelse(is.na(eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = "")))) == TRUE,
                 "",
                 "("),
          sprintf(
            ifelse(is.na(eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = "")))) == TRUE,
                   "%s",
                   "%.2f"), 
            ifelse(is.na(eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = "")))) == TRUE,
                   " ",
                   eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = ""))))
          ),
          ifelse(is.na(eval(parse(text = paste(model.list[m], "$std.error['", var.list[i], "']", sep = "")))) == TRUE,
                 "",
                 ")"), 
          sep = "", append = TRUE, file = tab.path
      )
    }
    , " \\\\ \n \\addlinespace[0.2cm] ",
    append = TRUE, file = tab.path
  )
  
}
#################################################################
#################################################################


#################################################################
#################################################################
## AIC SCORES
cat("\\midrule AIC", append = TRUE, file = tab.path)
for(m in 1:length(model.list)){
  cat(" & ", 
      sprintf("%.0f", eval(parse(text = paste(model.list[m], "_aic", sep = "")))),
      sep = "", append = TRUE, file = tab.path)
}
cat(" \\\\ \n", sep = "", append = TRUE, file = tab.path)
#################################################################
#################################################################

#################################################################
#################################################################
## OBSERVATIONS
cat("Observations", append = TRUE, file = tab.path)
for(m in 1:length(model.list)){
  cat(" & ", 
      sprintf("%.0f", eval(parse(text = paste(model.list[m], "$N", sep = "")))),
      sep = "", append = TRUE, file = tab.path)
}
cat(" \\\\ \n", sep = "", append = TRUE, file = tab.path)
#################################################################
#################################################################

cat("\\bottomrule %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \\end{tabular}
    \\end{adjustbox}
    \\caption*{\\footnotesize{\\textbf{Note:} OLS estimates with robust standard errors within parentheses. Higher values for updating implies manufacturing job increases and higher values for credibility means more credible predictions.  \\\\\\hspace{\\textwidth}
    $^* p <0.05, ^{**} p<0.01, ^{***} p<0.001$}}
    \\end{center}
    \\end{table}", 
    append = TRUE, file= tab.path)
